﻿'Description:任务单打印
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer:YangJiang
'create Date:2018-5-7
'Rewriter:
'Rewrite Date:

Imports System.Drawing
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports Camstar.WebClient.FormsBuilder.WebForm
Imports Camstar.WebClient.WebServicesProxy.InSiteWebServices
Imports System.IO
Imports Camstar.WebClient.WebConstants
Imports BusinessRule.IBusiness
Imports BusinessRule.DTO
Imports BusinessRule.BusinessFactory
Imports BusinessRule.Common
Imports System.Drawing.Printing.PrintPageEventArgs

Partial Class TaskBillManagePrint
    Inherits System.Web.UI.Page

    Dim ContainerID As String = ""
   
    Public strHtmlTable As String
    Public strHtmlTables(0 To 100) As String
    Dim sbHtml As New StringBuilder
    Private Shared Print_Type As String

    Private m_issueData As uLMSIssueERPMaterialIbusiness = uMESBusinessFactory.instance.GetuLMSIssueERPMaterialObject
    'Modify by Yuxj20180703
    Private m_ContainerObj As uLMSContainerIBusiness = uMESBusinessFactory.instance.GetuLMSContainerObject()

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not IsNothing(Session("DT")) Then
                Dim dt As DataTable = Session("DT")
                If Not IsNothing(Me.Request.Params("_Type")) Then
                    Print_Type = Me.Request.Params("_Type").ToString
                    Print_Type = Print_Type.ToString.Replace("?ServiceType=", "")
                End If
                iniPage(dt)
            End If

        End If
    End Sub

    Private Sub iniPage(ByVal dt As DataTable)
        Dim para As New Dictionary(Of String, String)
        Dim strHtmlTableMain As String
        strHtmlTableMain = SetTableMain(dt)
        strHtmlTable = String.Format("{0}{1}", strHtmlTable, strHtmlTableMain)
    End Sub

    ''' <summary>
    ''' 打印大零件（批次卡）和小零件（分初检装）
    ''' Modify by Yuxj20181113 如果大件没有，不打印批次卡，小件没有，不打印小件目录
    ''' </summary>
    ''' <param name="dtt"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function SetTableMain(ByVal dtt As DataTable) As String
        SetTableMain = String.Empty
        sbHtml.Clear()
        Dim para As New Dictionary(Of String, String)
        For Each dr As DataRow In dtt.Rows
            para("attributevalue") = dr("attributevalue")
            para("description") = dr("description")
            para("productname") = dr("productname")
            para("ContainerNo") = dr("ContainerNo")
            'Modify by Yuxj20180702增加批次ID,用于打印
            para("ContainerId") = dr("ContainerId")
            'Modify by Yuxj20180703判断初检装

            'Modify by Yuxj20181023增加对临时订单的打印
            Dim dt As New DataTable
            If dtt.Columns.Contains("MfgOrderName") Then
                dt = m_issueData.GetCollectErpMaterial(dr("MfgOrderName"))
                GetsbHtml(dt, "任务单", para)
                Continue For
            End If

            'Modify by Yuxj20181112 大小件分开打印 Print_Type：0:大小件，1:大件，2:小件  
            If Print_Type = "1" Then
                '只打印大件
                dt = m_issueData.GetTaskBillPrintInfo(dr("ContainerID"), "0", "")
                If dt.Rows.Count > 0 Then
                    GetsbHtml(dt, "批次卡", para)
                End If
                Continue For
            End If

            Dim FirstSecondIssue As String = String.Empty
            If Print_Type = "2" Then
                '只打印小件
                FirstSecondIssue = m_ContainerObj.CheckFirstSecondIssueNew(para("ContainerId"))
                If FirstSecondIssue = "2" Then
                    '检装
                    Dim dt2 = m_issueData.GetTaskBillPrintInfo(dr("ContainerID"), "1", "1")
                    If dt2.Rows.Count > 0 Then
                        GetsbHtml(dt2, "小零件目录(检装)", para)
                    End If

                Else
                    '都作为初装
                    Dim dt1 = m_issueData.GetTaskBillPrintInfo(dr("ContainerID"), "1", "2")
                    If dt1.Rows.Count > 0 Then
                        GetsbHtml(dt1, "小零件目录(初装)", para)
                    End If
                End If
                Continue For
            End If

            If Print_Type = "0" Then
                '打印大小件
                dt = m_issueData.GetTaskBillPrintInfo(dr("ContainerID"), "0", "")
                If dt.Rows.Count > 0 Then
                    GetsbHtml(dt, "批次卡", para)
                End If


                FirstSecondIssue = m_ContainerObj.CheckFirstSecondIssueNew(para("ContainerId"))
                If FirstSecondIssue = "2" Then
                    '检装
                    Dim dt2 = m_issueData.GetTaskBillPrintInfo(dr("ContainerID"), "1", "1")
                    If dt2.Rows.Count > 0 Then
                        GetsbHtml(dt2, "小零件目录(检装)", para)
                    End If

                Else
                    '都作为初装
                    Dim dt1 = m_issueData.GetTaskBillPrintInfo(dr("ContainerID"), "1", "2")
                    If dt1.Rows.Count > 0 Then
                        GetsbHtml(dt1, "小零件目录(初装)", para)
                    End If

                End If
            End If
            para.Clear()
        Next
        strHtmlTable = sbHtml.ToString
    End Function


    Private Sub GetsbHtml(ByVal dt As DataTable, ByVal strType As String, ByVal para As Dictionary(Of String, String))
        Dim strDate = Format(DateTime.Now, "yyyy年MM月dd日")
        Dim attributevalue = para("attributevalue")
        Dim description = para("description")
        Dim productname = para("productname")
        Dim ContainerNo = para("ContainerNo")
        'Modify by Yuxj20180702增加批次ID,用于打印
        Dim ContainerId = para("ContainerId")

        Dim dtCount = dt.Rows.Count
        Dim count = dtCount
        If dtCount <> 0 And dtCount Mod 34 = 0 Then
            count = dtCount - 1
        End If

        For j = 0 To count / 34
            sbHtml.AppendLine("<table width='1000' border='0' ><tr>")
            sbHtml.AppendLine("<td colspan='3' align='center'><font size='+3'>" + description + "" + strType + "</font></td>")
            sbHtml.AppendLine("</tr><tr><td width='454'>发动机号：" + attributevalue + "</td><td width='144' align='center'>部件号:" + productname + "</td>")
            sbHtml.AppendLine("<td width='388' align='center'>" + strDate + "</td></tr></table>")

            sbHtml.AppendLine("<div style='height:600px;'>")

            sbHtml.AppendLine("<table width='500' class='tab1' style='border-bottom:none'>")
            sbHtml.AppendLine("<tr><td width='40' style='border-bottom:none'>序号</td><td width='100' style='border-bottom:none'>零组件号</td><td width='50' style='border-bottom:none'>数量</td>")
            sbHtml.AppendLine("<td width='270' style='border-bottom:none'>名称</td><td width='90' style='border-bottom:none'>批次</td></tr>")

            For i = 0 + j * 34 To 16 + j * 34
                If 0 + j * 34 < dtCount And i < dtCount Then
                    Dim len = dt.Rows(i)("description").ToString.Length
                    sbHtml.AppendLine("<tr><td width='40'>" & (i + 1).ToString & "</td><td width='100'>" + dt.Rows(i)("productname").ToString + "</td><td width='50'>" + dt.Rows(i)("firstassembleqty").ToString + "</td>")
                    If len > 20 Then
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 10px'>" + dt.Rows(i)("description").ToString + "</font></td>")
                    ElseIf len > 15 Then
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 12px'>" + dt.Rows(i)("description").ToString + "</font></td>")
                    ElseIf len > 12 Then
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 13px'>" + dt.Rows(i)("description").ToString + "</font></td>")
                    Else
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 14px'>" + dt.Rows(i)("description").ToString + "</td>")
                    End If
                    sbHtml.AppendLine("<td width='90'>" + dt.Rows(i)("productbatchno").ToString + "</td></tr>")
                Else
                    sbHtml.AppendLine("<tr><td width='40'></td><td width='160'></td><td width='50'></td>")
                    sbHtml.AppendLine("<td width='270'></td><td width='90'></td></tr>")
                End If
            Next
            sbHtml.AppendLine("</table>")

            sbHtml.AppendLine("<table width='500' class='tab2' style='border-bottom:none;table-layout:fixed;'>")
            sbHtml.AppendLine("<tr><td width='40' style='border-bottom:none'>序号</td><td width='100' style='border-bottom:none'>零组件号</td><td width='50' style='border-bottom:none'>数量</td>")
            sbHtml.AppendLine("<td width='270' >名称</td><td width='90' style='border-bottom:none'>批次</td></tr>")
            For i = 17 + j * 34 To 33 + j * 34
                If 17 + j * 34 < dtCount And i < dtCount Then
                    Dim len = dt.Rows(i)("description").ToString.Length
                    sbHtml.AppendLine("<tr><td width='40'>" & (i + 1).ToString & "</td><td width='100'>" + dt.Rows(i)("productname").ToString + "</td><td width='50'>" + dt.Rows(i)("firstassembleqty").ToString + "</td>")
                    If len > 20 Then
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 10px'>" + dt.Rows(i)("description").ToString + "</font></td>")
                    ElseIf len > 15 Then
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 12px'>" + dt.Rows(i)("description").ToString + "</font></td>")
                    ElseIf len > 12 Then
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 13px'>" + dt.Rows(i)("description").ToString + "</font></td>")
                    Else
                        sbHtml.AppendLine("<td width='270' style='text-align: center;font-size: 14px'>" + dt.Rows(i)("description").ToString + "</td>")
                    End If
                    sbHtml.AppendLine("<td width='90'>" + dt.Rows(i)("productbatchno").ToString + "</td></tr>")
                Else
                    sbHtml.AppendLine("<tr><td width='40'></td><td width='160'></td><td width='50'></td>")
                    sbHtml.AppendLine("<td width='270'></td><td width='90'></td></tr>")
                End If
            Next
            sbHtml.AppendLine("</table>")
            sbHtml.AppendLine("</div>")
            sbHtml.AppendFormat(" <div class=barCode style='float:left；'>*{0}*</div> <div style='clear:both'></div>", ContainerId)  'Modify by Yuxj ContainerNo-->ContainerId
            sbHtml.AppendLine("<div style='page-break-after:always;'></div>")
        Next

    End Sub




End Class
